package cn.xk.order.service.impl;

import cn.xk.basic.util.LoginContext;
import cn.xk.basic.util.PageList;
import cn.xk.order.domain.OrderPetAcquisition;
import cn.xk.order.mapper.OrderPetAcquisitionMapper;
import cn.xk.order.query.OrderPetAcquisitionQuery;
import cn.xk.order.service.IOrderPetAcquisitionService;
import cn.xk.basic.service.impl.BaseServiceImpl;
import cn.xk.org.domain.Employee;
import cn.xk.org.domain.Shop;
import cn.xk.org.mapper.EmployeeMapper;
import cn.xk.org.mapper.ShopMapper;
import cn.xk.pet.domain.Pet;
import cn.xk.pet.domain.SearchMasterMsg;
import cn.xk.pet.dto.OrderDto;
import cn.xk.pet.mapper.PetMapper;
import cn.xk.pet.mapper.SearchMasterMsgMapper;
import cn.xk.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;

    @Override
    public PageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        //得到登录信息
        Logininfo logininfo = LoginContext.getLogininfo(request);
        //得到员工
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        //得到shopId用来区分是店铺管理员还是平台管理员
        Long shopId = employee.getShop_id();
        if (shopId != null) {
            //店铺人员
            Shop shop = shopMapper.loadById(shopId);
            if (!shop.getAdminId().equals(employee.getId())) {
                //店员
                query.setEmpId(employee.getId());
            }
            query.setShopId(shopId);
        }
        //5.平台管理员
        return super.queryPage(query);
    }

    @Override
    public void confirm(OrderDto orderDto) {

        //1.生成宠物信息
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(orderDto.getId());
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(order.getSearchMasterMsgId());
        Pet pet = msg2Pet(msg, orderDto.getMoney());
        petMapper.save(pet);
        //2.改订单状态为1，价格为成交价，支付方法
        order.setState(1);
        order.setPaytype(orderDto.getPayType());
        order.setPrice(orderDto.getMoney());
        order.setPetId(order.getPetId());
        orderPetAcquisitionMapper.update(order);
        //3.改寻主消息状态为2
        msg.setState(2);
        searchMasterMsgMapper.update(msg);


    }

    @Override
    public void cancel(Long id) {//订单id
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(id);
        //1.shopId置空
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());
        //3.改状态
        order.setState(-1);
        orderPetAcquisitionMapper.update(order);

    }

    private Pet msg2Pet(SearchMasterMsg msg, BigDecimal money) {
        Pet pet = new Pet();
        pet.setName(msg.getName());
        pet.setCostprice(money);
        pet.setTypeId(msg.getPetType());
        pet.setResources(msg.getResources());
        pet.setShopId(msg.getShopId());
        pet.setUserId(msg.getUserId());
        pet.setSearchMasterMsgId(msg.getId());
        return pet;
    }
}
